package models

import (
	"github.com/astaxie/beego/orm"
	"strconv"
)

type AiNotMatch struct {
	Id          int
	Tpl_id      int64
	Node_id     int64
	Words       string
	Create_time string
	Counts      int
	Uuid        string
	Record_file string
}

func (a *AiNotMatch) TableName() string {
	return TableName("not_match")
}

//strSql := "select a.node_id,a.words,a.counts,b.node_name from ai_not_match a,ai_process b where a.node_id=b.id and a.tpl_id="+strconv.FormatInt(tpl,10)

func GetAiNotMatchList(page, pageSize int, filters ...interface{}) ([]*AiNotMatch, int64) {
	offset := (page - 1) * pageSize
	list := make([]*AiNotMatch, 0)
	query := orm.NewOrm().QueryTable(TableName("not_match"))
	if len(filters) > 0 {
		l := len(filters)
		for k := 0; k < l; k += 2 {
			query = query.Filter(filters[k].(string), filters[k+1])
		}
	}
	total, _ := query.Count()
	query.OrderBy("-id").Limit(pageSize, offset).All(&list)

	return list, total
}

func DeleteNotMatchAllByTpl(tpl int64) error {
	sqlStr := "DELETE FROM ai_not_match WHERE tpl_id=" + strconv.FormatInt(tpl, 10)
	_, err := orm.NewOrm().Raw(sqlStr).Exec()
	if err != nil {
		return err
	}
	return nil
}

func GatNotMatchById(id int) (*AiNotMatch, error) {
	r := new(AiNotMatch)
	err := orm.NewOrm().QueryTable(TableName("not_match")).Filter("id", id).One(r)
	if err != nil {
		return nil, err
	}
	return r, nil
}

func (a *AiNotMatch) Delete() error {
	if _, err := orm.NewOrm().Delete(a); err != nil {
		return err
	}
	return nil
}


func DatchDelete(sql string) (error) {

	strSql := "DELETE FROM ai_not_match WHERE " + sql
	_,err := orm.NewOrm().Raw(strSql).Exec()

	if err != nil {
		return  err
	}
	return nil
}


func GetNodeName(id, tplid int64) (string, error) {

	strSql := "SELECT node_name FROM ai_process WHERE id=" + strconv.FormatInt(id, 10) + " AND owner_tpl_id=" + strconv.FormatInt(tplid, 10)

	var str = ""
	err := orm.NewOrm().Raw(strSql).QueryRow(&str)

	if err != nil {
		return str, err
	}
	return str, nil
}
